<!--
  * 系统设置 列表
  *
  * @Author:    admin
  * @Date:      2022-06-08 21:50:41
   
  
  * @Copyright  jiaoyuyun.org.cn
-->
<template>
  <div>
    <a-form class="smart-query-form">
      <a-row class="smart-query-form-row">
          <a-form-item label="用户名称" class="smart-query-form-item">
          <a-input style="width: 300px" v-model:value="queryForm.name" placeholder="请输入用户名称" />
        </a-form-item>
        <a-form-item label="用户昵称" class="smart-query-form-item">
          <a-input style="width: 300px" v-model:value="queryForm.petName" placeholder="请输入用户昵称" />
        </a-form-item>
        <a-form-item label="学校名称" class="smart-query-form-item">
          <a-input style="width: 300px" v-model:value="queryForm.schoolName" placeholder="请输入学校名称" />
        </a-form-item>
        <a-form-item class="smart-query-form-item smart-margin-left10">
          <!-- v-privilege="'support:shop:query'" -->
          <a-button type="primary" @click="onSearch" v-privilege="'support:user:query'">
            <template #icon>
              <SearchOutlined />
            </template>
            查询
          </a-button>
          <!-- v-privilege="'support:shop:query'" -->
          <a-button @click="resetQuery" v-privilege="'support:shop:query'" class="smart-margin-left10">
            <template #icon>
              <ReloadOutlined />
            </template>
            重置
          </a-button>
<!-- v-privilege="'support:shop:add'" -->
          <a-button @click="toEditOrAdd()" type="primary" v-privilege="'support:shop:add'" class="smart-margin-left20">
            <template #icon>
              <PlusOutlined />
            </template>
            新建
          </a-button>
        </a-form-item>
        <a-form-item label="快速筛选" class="smart-query-form-item">
        <a-radio-group v-model:value="queryForm.del" @change="onSearch">
          <a-radio-button :value="undefined">在册</a-radio-button>
          <a-radio-button :value="true">已注销</a-radio-button>
        </a-radio-group>
      </a-form-item>
      </a-row>
    </a-form>

    <a-card size="small" :bordered="true" :hoverable="true">
      <a-row justify="end">
        <TableOperator class="smart-margin-bottom5" v-model="columns" :tableId="TABLE_ID_CONST.SUPPORT.USER" :refresh="ajaxQuery" />
      </a-row>

      <a-table :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
       size="small" 
       :loading="tableLoading" 
       bordered 
       :dataSource="tableData" 
       :columns="columns" 
       row-Key="id" 
       :pagination="false">
        <template #bodyCell="{ column, text, record, index }">
          <span v-if="column.dataIndex === 'serialNumber'">{{ index + 1 }}</span>
          <template v-if="column.dataIndex === 'action'">
            <div class="smart-table-operate" v-if="record.del === false">
              <a-button @click="toEditOrAdd(record)" v-privilege="'support:user:update'" type="link">编辑</a-button>
              <a-button @click="deleteShops(record)" v-privilege="'support:user:delete'" danger type="link">辞退</a-button>
            </div>
          </template>
            <template v-if="column.dataIndex === 'action'">
            <div class="smart-table-operate" v-if="record.del === true">
              <a-button @click="reset(record)" v-privilege="'support:user:update'" type="link">重新启用</a-button>
            </div>
          </template>
          <template v-if="column.dataIndex === 'gender'">
            <div class="smart-table-operate">
              <div v-if="record.gender===1">男</div>
              <div v-if="record.gender===2">女</div>
            </div>
          </template>
          <template v-if="column.dataIndex==='name'">
              <div class="smart-table-operate">
                <a @click="showValueList(record.userId)">{{ record.name }}</a>
              </div>
          </template>
        </template>
      </a-table>
      <div class="smart-query-table-page">
        <a-pagination
          showSizeChanger
          showQuickJumper
          show-less-items
          :pageSizeOptions="PAGE_SIZE_OPTIONS"
          :defaultPageSize="queryForm.pageSize"
          v-model:current="queryForm.pageNum"
          v-model:pageSize="queryForm.pageSize"
          :total="total"
          @change="ajaxQuery"
          @showSizeChange="ajaxQuery"
          :show-total="(total) => `共${total}条`"
        />
      </div>
    </a-card>
    <FormModal ref="config" @reloadList="resetQuery" />
    <DictValueModal ref="dictValueModal" />

  </div>
</template>
<script setup>
  import { onMounted, reactive, ref } from 'vue';
  import { message, Modal } from 'ant-design-vue';
  import { SmartLoading } from '/@/components/framework/smart-loading';
  import DictValueModal from './jdy-modal.vue';
  import _ from 'lodash';
  import { userApi } from '/@/api/support/user-api';
  import FormModal from './jdy-edit.vue';
  import { PAGE_SIZE_OPTIONS } from '/@/constants/common-const';
  import { smartSentry } from '/@/lib/smart-sentry';
  import TableOperator from '/@/components/support/table-operator/index.vue';
  import { TABLE_ID_CONST } from '/@/constants/support/table-id-const';

  const columns = ref([
    {
      title: '序号',
      dataIndex: 'serialNumber', // 这个dataIndex仅作为标识，不对应实际数据字段
      width: '80px', // 可以自定义宽度
      scopedSlots: { customRender: 'bodyCell' }, // 对应模板中的具名插槽
      align:"center"
    },
    // {
    //   title: '用户编号',
    //   width: 100,
    //   dataIndex: 'id',
    //   align:"center",

    // },
    {
      title: '用户名称',
      dataIndex: 'name',
      ellipsis: true,
      align:"center",
    },
    {
      title: '用户昵称',
      dataIndex: 'petName',
      ellipsis: true,
      align:"center",
    },
    {
      title: '性别',
      dataIndex: 'gender',
      ellipsis: true,
      width: 150,
      align:"center",
    },
     {
      title: '学校名称',
      dataIndex: 'schoolName',
      ellipsis: true,
      align:"center",
    },
     {
      title: '专业名称',
      dataIndex: 'majorName',
      ellipsis: true,
      align:"center",
    },
    {
      title: '学号',
      dataIndex: 'idCard',
      ellipsis: true,
      align:"center",
      
    },
     {
      title: '联系电话',
      dataIndex: 'Phone',
      ellipsis: true,
      align:"center",
      
    },       
    {
      title: '用户头像',
      dataIndex: 'picture',
      ellipsis: true,
      align:"center",
      className:'tableHiddle'
      
    },
    {
      title: '修改时间',
      dataIndex: 'updateTime',
      width: 150,
      align:"center",
    },
 {
      title: '更新者',
      dataIndex: 'updateBy',
      ellipsis: true,
      align:"center",
    },
    {
      title: '操作',
      dataIndex: 'action',
      fixed: 'right',
      width: 60,
      align:"center",
    },
  ]);

  // ---------------- 查询数据 -----------------------
  // function onSelectChange(keyArray, selectRows) {
  //   selectedRowKeys.value = keyArray;
  //   selectedRows.value = selectRows;
  // }
  const queryFormState = {
    userId: undefined,
    name: '',
    petName:'',
    userType:2,
    pageNum: 1,
    pageSize: 10,
  };
  const queryForm = reactive({ ...queryFormState });

  const tableLoading = ref(false);
  const tableData = ref([]);
  const total = ref(0);

  function resetQuery() {
    Object.assign(queryForm, queryFormState);
    ajaxQuery();
  }

  function onSearch() {
    queryForm.pageNum = 1;
    ajaxQuery();
  }
  const dictValueModal = ref();
  function showValueList(jdyId) {
    dictValueModal.value.showModal(jdyId);
  }
  async function ajaxQuery() {
    try {
      tableLoading.value = true;
      //调用查询方法
      let responseModel = await userApi.queryJdy(queryForm);
      const list = responseModel.data.list;      
      // for (const item of list)
      // {
      //   item.shopRangeName = _.join(item.shopRangeName, ',');
      // }
      // console.info('list='+JSON.stringify(list))
      total.value = responseModel.data.total;
      tableData.value = list;
      // // 清除选中
      // selectedRowKeys.value = [];
      // selectedRows.value = [];
    } catch (e) {
      smartSentry.captureError(e);
    } finally {
      tableLoading.value = false;
    }
  }

  // ------------------------- 表单操作 弹窗 ------------------------------
  let selectedRowKeys = ref([]);
  let selectedRows = ref([]);
  const config = ref();
  function toEditOrAdd(rowData) {
    config.value.showModal(rowData);
  }
  function deleteShops(userData) {
    Modal.confirm({
      title: '提示',
      content: '确定要解除【' + userData.name + '】的接单员身份吗?',
      okText: '确定',
      okType: 'danger',
      onOk() {
        singleDelete(userData);
      },
      cancelText: '取消',
      onCancel() {},
    });
  }

  async function singleDelete(userData) {
    try {
      SmartLoading.show();
      userData.userType=1;
      await userApi.updateUser(userData);
      message.success('解除成功');
      queryData();
    } catch (e) {
      smartSentry.captureError(e);
    } finally {
      SmartLoading.hide();
      resetQuery();
    }
  }
function reset(userData) {
    Modal.confirm({
      title: '提示',
      content: '确定要恢复【' + userData.petName + '】的使用吗?',
      okText: '确定',
      okType: 'danger',
      onOk() {
        singlereset(userData);
      },
      cancelText: '取消',
      onCancel() {},
    });
  }

  async function singlereset(userData) {
    try {
      SmartLoading.show();
      userData.del=0;
      await userApi.updateUser(userData);
      message.success('重启成功');
      queryData();
    } catch (e) {
      smartSentry.captureError(e);
    } finally {
      SmartLoading.hide();
      resetQuery();
    }
  }
  onMounted(ajaxQuery);
</script>
<style>
.tableHiddle {
  display: none;
}
.tableShow{
  display: revert;
}
</style>